package com.sgb.kjwl.view.ui.fragment.manage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrDemandAnnouncementChildBinding;
import com.sgb.kjwl.databinding.FrDemandAskLeaseChildBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.kjwl.model.entity.lease.AskLeaseSheBeiEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseWebsiteEntity;
import com.sgb.kjwl.view.ui.adapter.machine.AskLeaseAdapter;
import com.sgb.kjwl.view.ui.adapter.machine.AskLeaseWebsiteAdapter;
import com.sgb.kjwl.viewmodel.manage.ChildForDemandAnnouncementModel;
import com.sgb.kjwl.viewmodel.manage.ChildForDemandAskLeaseModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 *
 */
/*
 * Author LiWei
 * Create Time 2021/4/15
 * Description 网站-需求公告-租赁需求
 * */
public class ChildForDemandAskLeaseFragment extends AppLazyFragment {

    @Inject
    ChildForDemandAskLeaseModel viewModel;
    private FrDemandAskLeaseChildBinding binding;
    private AskLeaseWebsiteAdapter adapter;
    private String websiteNo;
    private String websiteType;
    private int page = 1;
    private boolean isLoadMore = false;

    public static ChildForDemandAskLeaseFragment newInstance(String websiteNo,String websiteType) {
        ChildForDemandAskLeaseFragment fragment = new ChildForDemandAskLeaseFragment();
        Bundle args = new Bundle();
        args.putString("websiteNo", websiteNo);
        args.putString("websiteType", websiteType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        super.setupFragmentComponent(appComponent);
        DaggerEnterpriseManageFragComponent.builder().appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_demand_ask_lease_child, container, false);
        if (getArguments() != null) {
            websiteNo = getArguments().getString("websiteNo");
            websiteType = getArguments().getString("websiteType");
        }
        return binding.getRoot();
    }


    @Override
    protected void initView() {
        adapter = new AskLeaseWebsiteAdapter(getActivity(), null);
//        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0,
//                getActivity().getResources().getDimension(R.dimen.dp2),
//                getActivity().getResources().getColor(R.color.color_F5F5F5)));
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                page = 1;
                initFilterData();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page = page + 1;
                initFilterData();
            }
        });

        adapter.setOnItemClick(new AdapterOnItemClick<AskLeaseWebsiteEntity>() {
            @Override
            public void onItemClick(AskLeaseWebsiteEntity entity, int position) {
                if (!ClickUtils.isDoubleClick()) {
                    viewModel.getH5Url(Integer.parseInt(entity.getType()), entity.getNo());
                }
            }
        });
    }


    @Override
    protected void onLazyLoad() {
        initFilterData();
    }

    private void initFilterData() {
        viewModel.getDataList(websiteNo, websiteType, page);
    }

    public void showList(List<AskLeaseWebsiteEntity> list) {
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (null != list && list.size() > 0) {
                adapter.addDatas(list);
            } else {
                binding.recyclerView.setLoadingMoreEnabled(false);
            }
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                adapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

}
